﻿//===============================================================================
//	Backpack.cpp
//	用户背包
//===============================================================================

#include "Backpack.h"
#include "../share/user_define.h"
#include "../../3rd/libjson/json.h"

//-------------------------------------------------------------------------------
//	背包物品类
BackpackItem::BackpackItem(void)
	: m_id(0)
	, m_count(0)
	, m_max_count(0)
	, m_types(0)
{
}

void BackpackItem::Clear(void)
{
	m_id = 0;
	m_count = 0;
	m_max_count = 0;
	m_types = 0;
	memset(m_uuid, 0, 48);
}

BackpackItem & BackpackItem::operator=(const BackpackItem & rhs)
{
	Clear();
	
	m_id = rhs.m_id;
	m_count = rhs.m_count;
	m_max_count = rhs.m_max_count;
	m_types = rhs.m_types;
	memcpy(m_uuid, rhs.m_uuid, sizeof(m_uuid));

	return *this;
}

//-------------------------------------------------------------------------------
Backpack::Backpack(void)
	: m_end_pos(0)
{
	_safe_guard_begin;
	_safe_guard_end;
}

//-------------------------------------------------------------------------------
Backpack::~Backpack(void)
{
}

//-------------------------------------------------------------------------------
bool Backpack::Initialize(void)
{
	_safe_guard_begin;

	for (auto &_it : m_items_array)
	{
		_it.Clear();
	}

	return true;

	_safe_guard_end;

	return false;
}

//-------------------------------------------------------------------------------
int16_t Backpack::GetItemCountByItemId(uint32_t id)
{
	_safe_guard_begin;

	for (int16_t i = 0; i < m_end_pos; ++i)
	{
		if (m_items_array[i].m_id == id)
		{
			return m_items_array[i].m_count;
		}
	}

	return 0;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
int16_t Backpack::GetItemIndexByItemId(uint32_t id)
{
	_safe_guard_begin;

	for (int16_t i = 0; i < m_end_pos; ++i)
	{
		if (m_items_array[i].m_id == id)
		{
			return i;
		}
	}

	return 0;

	_safe_guard_end;

	return -1;
}

//-------------------------------------------------------------------------------
void Backpack::SetItemCountByItemId(uint32_t id, int16_t count)
{
	_safe_guard_begin;

	int16_t _idx = GetItemIndexByItemId(id);
	if (_idx > 0)
	{
		m_items_array[_idx].m_count = count;
	}

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void Backpack::SetItemInfo(uint32_t id, int16_t count, int16_t maxCount
	, int16_t types, std::string uuid)
{
	_safe_guard_begin;

	if (m_end_pos >= max_backpack_item_count)
	{
		return;
	}

	int16_t _idx = GetItemIndexByItemId(id);
	if (0 == _idx)
	{
		_idx = m_end_pos;
		++m_end_pos;
	}

	m_items_array[_idx].Clear();
	m_items_array[_idx].m_id = id;
	m_items_array[_idx].m_count = count;
	m_items_array[_idx].m_max_count = maxCount;
	m_items_array[_idx].m_types = types;
	memcpy(m_items_array[_idx].m_uuid, uuid.c_str(), 48);

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
bool Backpack::DeleteItemByItemId(uint32_t id)
{
	_safe_guard_begin;

	int16_t _idx = GetItemIndexByItemId(id);
	if (-1 == _idx)
	{
		return true;
	}

	--m_end_pos;

	m_items_array[_idx] = m_items_array[m_end_pos];
	m_items_array[m_end_pos].Clear();

	return true;

	_safe_guard_end;

	return false;
}

std::string Backpack::GetItemListJsonString(void)
{
	_safe_guard_begin;

	Json::Value vv;
	vv.clear();

	for (int16_t i = 0; i < m_end_pos; ++i)
	{
		Json::Value _jm;
		_jm.clear();

		_jm["id"] = Json::Value(m_items_array[i].m_id);
		_jm["at"] = Json::Value(m_items_array[i].m_count);
		_jm["uid"] = Json::Value(m_items_array[i].m_uuid);

		vv.append(_jm);
	}

	return vv.asString();

	_safe_guard_end;
}

std::string Backpack::GetItemListJsonStringByTypes(uint16_t types)
{
	_safe_guard_begin;

	Json::Value vv;
	vv.clear();

	for (int16_t i = 0; i < m_end_pos; ++i)
	{
		if (m_items_array[i].m_types == types)
		{
			Json::Value _jm;

			_jm.clear();

			_jm["id"] = Json::Value(m_items_array[i].m_id);
			_jm["at"] = Json::Value(m_items_array[i].m_count);
			_jm["uid"] = Json::Value(m_items_array[i].m_uuid);

			vv.append(_jm);
		}
	}

	return vv.asString();

	_safe_guard_end;
}



//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
